using Microsoft.EntityFrameworkCore;
using WorkOrder.Contexts;
using WorkOrder.Models;

public class DashboardService
{
    private readonly WorkOrderContext _context;

    public DashboardService(WorkOrderContext context)
    {
        _context = context;
    }

public async Task<DashboardData> GetDashboardDataAsync(DateTime date)
{
    var startOfYear = new DateTime(date.Year, 1, 1);
    var startOfMonth = new DateTime(date.Year, date.Month, 1);
    var startOfDay = date.Date;
    var endOfDay = startOfDay.AddDays(1);

    // 今日完成量
    var completedToday = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已解决 && o.关闭时间 >= startOfDay && o.关闭时间 < endOfDay);

    // 今日失败量
    var failedToday = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已关闭 && o.关闭时间 >= startOfDay && o.关闭时间 < endOfDay);

    // 今日用户成功率
    var totalProcessedToday = completedToday + failedToday;
    var userSuccessRateToday = totalProcessedToday > 0 ? (decimal)completedToday / totalProcessedToday * 100 : 0;

    // 当月总流水
    var totalAmountThisMonth = await _context.资金流水表s
        .Where(o => o.创建时间 >= startOfMonth && o.创建时间 < startOfMonth.AddMonths(1) && o.交易状态== FlowStatus.SUCCESS && o.流水类型== FlowType.消费)
        .SumAsync(o => o.变动金额);

    // 今日流水
    var totalAmountToday = await _context.资金流水表s
        .Where(o => o.创建时间 >= startOfDay && o.创建时间 < endOfDay && o.交易状态== FlowStatus.SUCCESS && o.流水类型== FlowType.消费)
        .SumAsync(o => o.变动金额);

    // 年流水
    var totalAmountThisYear = await _context.资金流水表s
        .Where(o => o.创建时间 >= startOfYear && o.创建时间 < startOfYear.AddYears(1) && o.交易状态== FlowStatus.SUCCESS && o.流水类型== FlowType.消费)
        .SumAsync(o => o.变动金额);

    // 月完成量
    var completedThisMonth = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已解决 && o.关闭时间 >= startOfMonth);

    // 月失败量
    var failedThisMonth = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已关闭 && o.关闭时间 >= startOfMonth);

    // 年完成量
    var completedThisYear = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已解决 && o.关闭时间 >= startOfYear);

    // 年失败量
    var failedThisYear = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已关闭 && o.关闭时间 >= startOfYear);

    return new DashboardData
    {
        TotalOrders = await _context.工单主表s.CountAsync(),
        SignedOrders = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已解决),
        YearOrders = await _context.工单主表s.CountAsync(o => o.创建时间 >= startOfYear),
        MonthOrders = await _context.工单主表s.CountAsync(o => o.创建时间 >= startOfMonth),
        DayOrders = await _context.工单主表s.CountAsync(o => o.创建时间 >= startOfDay),
        CompletedToday = completedToday,
        FailedToday = failedToday,
        UserSuccessRateToday = userSuccessRateToday,
        TotalAmountThisMonth = (decimal)totalAmountThisMonth,
        TotalAmountToday = (decimal)totalAmountToday,
        TotalAmountThisYear = (decimal)totalAmountThisYear,
        CompletedThisMonth = completedThisMonth,
        FailedThisMonth = failedThisMonth,
        CompletedThisYear = completedThisYear,
        FailedThisYear = failedThisYear
    };

}
// 所有负责人得用户完成率
public async Task<Dictionary<string, decimal>> GetUserSuccessRatesAsync(DateTime date)
{
    var startOfDay = date.Date;
    var endOfDay = startOfDay.AddDays(1);

    // 获取所有负责人
    var users = await _context.工单主表s
        .Where(o => o.关闭时间 >= startOfDay && o.关闭时间 < endOfDay)
        .Select(o => o.创建人id)
        .Distinct()
        .ToListAsync();

    var userSuccessRates = new Dictionary<string, decimal>();

    foreach (var user in users)
    {
        var completedCount = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已解决 && o.创建人id == user && o.关闭时间 >= startOfDay && o.关闭时间 < endOfDay);
        var failedCount = await _context.工单主表s.CountAsync(o => o.当前状态 == WorkOrderStatus.已关闭 && o.创建人id == user && o.关闭时间 >= startOfDay && o.关闭时间 < endOfDay);
        var totalProcessed = completedCount + failedCount;
        var successRate = totalProcessed > 0 ? (decimal)completedCount / totalProcessed * 100 : 0;
        userSuccessRates[user] = successRate;
    }

    return userSuccessRates;
}
public class DashboardData
{
    public int TotalOrders { get; set; } // 总工单数量
    public int SignedOrders { get; set; } // 已签发工单数量
    public int YearOrders { get; set; } // 年工单数量
    public int MonthOrders { get; set; } // 月工单数量
    public int DayOrders { get; set; } // 日工单数量

    // 新增统计数据
    public int CompletedToday { get; set; } // 今日完成量
    public int FailedToday { get; set; } // 今日失败量
    public decimal UserSuccessRateToday { get; set; } // 今日用户成功率
    public decimal TotalAmountThisMonth { get; set; } // 当月总流水
    public decimal TotalAmountToday { get; set; } // 今日流水
    public decimal TotalAmountThisYear { get; set; } // 年流水

    public int CompletedThisMonth { get; set; } // 月完成量
    public int FailedThisMonth { get; set; } // 月失败量
    public int CompletedThisYear { get; set; } // 年完成量
    public int FailedThisYear { get; set; } // 年失败量

}}



